package cn.nju.zyy.model.person;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="PERSONS")
public class Person {

	private int id;
	private String firstName;
	private String lastName;
	private String email;
	private String phoneNumber;
	private String sport;
	private String gender;
	private String residency;
	private boolean over21;
	private Set<String> carModels;
	private int age;

	public Person() {
		id = -1;
		carModels = new HashSet<String>();
	};

	public Person(int id, String firstName, String lastName, String email,
			String phoneNumber, String sport, String gender, String residency,
			boolean over21, String[] carModels) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.phoneNumber = phoneNumber;
		this.sport = sport;
		this.gender = gender;
		this.residency = residency;
		this.over21 = over21;
		this.carModels = new HashSet<String>();
		for(String str : carModels) {
			this.carModels.add(str);
		}
	}

	@Id
	@GeneratedValue
	@Column(name="PERSON_ID")
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name="FIRST_NAME", nullable=false)
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@Column(name="LAST_NAME", nullable=false)
	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Column(name="EMAIL", nullable=false)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name="PHONE_NUMBER", nullable=false)
	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	@Column(name="SPORT", nullable=false)
	public String getSport() {
		return sport;
	}

	public void setSport(String sport) {
		this.sport = sport;
	}

	@Column(name="GENDER", nullable=false)
	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	@Column(name="RESIDENCY", nullable=false)
	public String getResidency() {
		return residency;
	}

	public void setResidency(String residency) {
		this.residency = residency;
	}

	@Column(name="IS_OVER_21", nullable=false)
	public boolean isOver21() {
		return over21;
	}

	public void setOver21(boolean over21) {
		this.over21 = over21;
	}

	@ElementCollection
	@CollectionTable(name="CAR_MODELS", joinColumns=@JoinColumn(name="PERSON_ID"))
	@Column(name="CAR_MODEL")
	public String[] getCarModels() {
		return (String[]) carModels.toArray();
	}

	public void setCarModels(String[] carModels) {
		this.carModels.clear();
		for(String str : carModels) {
			this.carModels.add(str);
		}
	}

	@Column(name="AGE", nullable=false)
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "First name: " + getFirstName() + " | Last name: "
				+ getLastName() + " | Favorite Sport: " + getSport()
				+ " | Gender: " + getGender() + " | Residency: "
				+ getResidency() + " | Over 21: " + isOver21()
				+ " | Car models: " + Arrays.asList(getCarModels())
				+ " | Email: " + getEmail() + " | Age: " + getAge();
	}

}
